/*
 * @Author: your name
 * @Date: 2021-04-16 19:51:22
 * @LastEditTime: 2021-04-17 10:15:37
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /flutter_demo/lib/pages/RenderData.dart
 */
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:dio/dio.dart';

class RenderDataPage extends StatefulWidget {
  @override
  _RenderDataPageState createState() => _RenderDataPageState();
}

class _RenderDataPageState extends State<RenderDataPage> {
  List _dataList = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    this._getStaffData();
  }

  _getStaffData() async {
    try {
      Response response = await Dio().get(
          'http://rap2api.taobao.org/app/mock/276191/example/1611191337482');
      // print(response.data);
      setState(() {
        _dataList = response.data['staffData'];
      });
    } catch (e) {
      print(e);
    }
  }

  // _getStaffData() async {
  //   var apiUrl = Uri.parse(
  //       'http://rap2api.taobao.org/app/mock/276191/example/1611191337482');
  //   var result = await http.get(apiUrl);
  //   if (result.statusCode == 200) {
  //     setState(() {
  //       _dataList = json.decode(result.body)['staffData'];
  //       print(_dataList);
  //     });
  //   } else {
  //     print(result.statusCode);
  //   }
  // }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('渲染数据'),
        ),
        body: this._dataList.length > 0
            ? ListView.builder(
                itemCount: this._dataList.length,
                itemBuilder: (context, index) {
                  return ListTile(
                    title: Text('${this._dataList[index]['username']}'),
                  );
                })
            // ListView(
            //     children: this._dataList.map((item) {
            //     return ListTile(
            //       title: Text(item['username']),
            //     );
            //   }).toList())
            : Text('加载中...'));
  }
}
